package com.lexue.dao.impl;

import java.util.List;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Disjunction;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.Order;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.lexue.dao.ITeacherInfo;
import com.lexue.domain.SysUser;
import com.lexue.domain.TeacherInfo;

/**
 * A data access object (DAO) providing persistence and search support for
 * TeacherInfo entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.lexue.dao.impl.TeacherInfo
 * @author MyEclipse Persistence Tools
 */

public class TeacherInfoDAO extends HibernateDaoSupport implements ITeacherInfo {
	private static final Log log = LogFactory.getLog(TeacherInfoDAO.class);
	// property constants
	public static final String TEACHER_NAME = "teacherName";
	public static final String CARD_TYPE = "cardType";
	public static final String CARD_NUMBER = "cardNumber";
	public static final String SEX = "sex";
	public static final String SCHOOL = "school";
	public static final String SYSUSER = "sysUser";
	/*
	 * (non-Javadoc)
	 * 
	 * @see com.lexue.dao.impl.ITeacherInfo#save(com.lexue.domain.TeacherInfo)
	 */
	public void save(TeacherInfo transientInstance) {
		log.debug("saving TeacherInfo instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.lexue.dao.impl.ITeacherInfo#delete(com.lexue.domain.TeacherInfo)
	 */
	public void delete(TeacherInfo persistentInstance) {
		log.debug("deleting TeacherInfo instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.lexue.dao.impl.ITeacherInfo#findById(java.lang.Integer)
	 */
	public TeacherInfo findById(java.lang.Integer id) {
		log.debug("getting TeacherInfo instance with id: " + id);
		try {
			TeacherInfo instance = (TeacherInfo) getHibernateTemplate().get(
					"com.lexue.domain.TeacherInfo", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	
	
	
//	public TeacherInfo findByUserId(java.lang.Integer user_id) {
//		log.debug("getting TeacherInfo instance with user_id: " + user_id);
//		try {
//			String queryString = "from TeacherInfo as model where model.user_id="+user_id;
//			List result = this.getHibernateTemplate().find(queryString);
//			if(result!=null&&result.size()>0){
//				return (TeacherInfo)result.get(0);
//			}
//				return null;
//			}
//		} catch (RuntimeException re) {
//			log.error("find by user_id failed", re);
//			throw re;
//		}
//	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.lexue.dao.impl.ITeacherInfo#findByExample(com.lexue.domain.TeacherInfo
	 * )
	 */
	public List findByExample(TeacherInfo instance) {
		log.debug("finding TeacherInfo instance by example");
		try {
			List results = getHibernateTemplate().getSessionFactory()
					.openSession().createCriteria(
							"com.lexue.domain.TeacherInfo").add(
							Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.lexue.dao.impl.ITeacherInfo#findByProperty(java.lang.String,
	 * java.lang.Object)
	 */
	public List findByProperty(String propertyName, Object value) {
		log.debug("finding TeacherInfo instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from TeacherInfo as model where model."
					+ propertyName + "= ?";
			Query queryObject = getHibernateTemplate().getSessionFactory()
					.openSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}
	
	public List findBySysUser(Object sysUser) {
		return findByProperty(SYSUSER, sysUser);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.lexue.dao.impl.ITeacherInfo#findByTeacherName(java.lang.Object)
	 */
	public List findByTeacherName(Object teacherName) {
		return findByProperty(TEACHER_NAME, teacherName);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.lexue.dao.impl.ITeacherInfo#findByCardType(java.lang.Object)
	 */
	public List findByCardType(Object cardType) {
		return findByProperty(CARD_TYPE, cardType);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.lexue.dao.impl.ITeacherInfo#findByCardNumber(java.lang.Object)
	 */
	public List findByCardNumber(Object cardNumber) {
		return findByProperty(CARD_NUMBER, cardNumber);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.lexue.dao.impl.ITeacherInfo#findBySex(java.lang.Object)
	 */
	public List findBySex(Object sex) {
		return findByProperty(SEX, sex);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.lexue.dao.impl.ITeacherInfo#findBySchool(java.lang.Object)
	 */
	public List findBySchool(Object school) {
		return findByProperty(SCHOOL, school);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.lexue.dao.impl.ITeacherInfo#findAll()
	 */
	public List findAll() {
		log.debug("finding all TeacherInfo instances");
		try {
			String queryString = "from TeacherInfo";
			Query queryObject = getHibernateTemplate().getSessionFactory()
					.openSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.lexue.dao.impl.ITeacherInfo#merge(com.lexue.domain.TeacherInfo)
	 */
	public TeacherInfo merge(TeacherInfo detachedInstance) {
		log.debug("merging TeacherInfo instance");
		try {
			TeacherInfo result = (TeacherInfo) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}
	
	public void update(Integer teacherId,String teacherName,String sex,String cardType,String cardNumber,String school){
		TeacherInfo teacherInfo=(TeacherInfo)getHibernateTemplate().get(TeacherInfo.class, teacherId);
		teacherInfo.setTeacherName(teacherName);
		teacherInfo.setSex(sex);
		teacherInfo.setCardType(cardType);
		teacherInfo.setCardNumber(cardNumber);
		teacherInfo.setSchool(school);
		this.getHibernateTemplate().update(teacherInfo);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.lexue.dao.impl.ITeacherInfo#attachDirty(com.lexue.domain.TeacherInfo)
	 */
	public void attachDirty(TeacherInfo instance) {
		log.debug("attaching dirty TeacherInfo instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.lexue.dao.impl.ITeacherInfo#attachClean(com.lexue.domain.TeacherInfo)
	 */
	public void attachClean(TeacherInfo instance) {
		log.debug("attaching clean TeacherInfo instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public List searchBySubject(List subject, String way, String minPrice,
			String maxPrice, String time, String sex, String school,
			String city, String area, String isMinToMax) {
		try {
			Criteria crit = getSession().createCriteria(TeacherInfo.class);

			// check minPrice and add its expression
			Criteria subCrit = crit.createCriteria("subjects");

			try {
				float fMinPrice = Float.parseFloat(minPrice);
				System.err.println("Min_Price: " + fMinPrice);
				subCrit.add(Expression.ge(SubjectDAO.SUBJECT_PRICE, new Float(
						fMinPrice)));
			} catch (Exception e) {
			}

			try {
				float fMaxPrice = Float.parseFloat(maxPrice);
				System.err.println("Max_Price: " + fMaxPrice);
				subCrit.add(Expression.le(SubjectDAO.SUBJECT_PRICE, new Float(
						fMaxPrice)));
			} catch (Exception e) {
			}

			if (subject != null && subject.size() > 0 ) {
				Disjunction dsj = Expression.disjunction();

				for (int i = 0; i < subject.size(); i++) {
					if(((String)subject.get(i)).length()>0)
						dsj.add(Expression.eq(SubjectDAO.SUBJECT_NAME, subject
							.get(i)));
				}
				subCrit.add(dsj);
			}

			// check way and add its expression
			if( way != null && way.length()>0)
				subCrit.add(Expression.eq(SubjectDAO.SUBJECT_WAY, way));				

			// check time and add its expression
			if( time != null && time.length()>0) 
			{
				subCrit.add(Expression.eq(SubjectDAO.SUBJECT_TIME, time));
			}

			// isMinToMax

			if ("false".equalsIgnoreCase(isMinToMax)) {
				subCrit.addOrder(Order.desc(SubjectDAO.SUBJECT_PRICE));
			} else {
				// default order
				subCrit.addOrder(Order.asc(SubjectDAO.SUBJECT_PRICE));
			}

			// check sex and add its expression
			if (sex != null
					&& (sex.equalsIgnoreCase("male") || sex
							.equalsIgnoreCase("female"))) {
				crit.add(Expression.eq(SEX, sex));
			}

			// check school and add its expression
			if(school != null && school.length()>0)
			
				crit.add(Expression.eq(SCHOOL, school));

			// city
			if(city != null && city.length()>0)
			{
					crit.createCriteria("city").add(Expression.eq(CityDAO.CITY_NAME, city));			
			}

			// area
			if(area != null && area.length()>0)
			{
					crit.createCriteria("area").add(Expression.eq(AreaDAO.AREA_NAME, area));
			}

			return crit.list();

		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}

	}

	public TeacherInfo queryById(java.lang.Integer id) {
		try {
			TeacherInfo instance = null;
			Criteria crit = getSession().createCriteria(TeacherInfo.class);
			Criteria subCrit = crit.createCriteria("subjects");
			crit.add(Expression.eq("teacherId", id));
			List l = crit.list();
			if(l.size()>0)
				instance = (TeacherInfo)l.get(0);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
}